package com.test.util.ok;

import cn.hutool.core.net.NetUtil;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;

@Component
public class MySnowFlake {
    private long lastTimestamp = -1L;
    private int sequence;
    private final int sequenceMask=999999;
    public MySnowFlake(){
        sequence = 1;
        if (sequence<0||sequence>sequenceMask){
            throw new IllegalArgumentException(
                    "sequence can't be greater than 999 or lesser than 0"
            );
        }
        this.sequence=sequence;

    }
    public synchronized String nextId() {
        String result="";
        long timestamp = timeGen();
        if (timestamp < lastTimestamp) {
            System.err.printf(
                    "clock is moving backwards. Rejecting requests until %d.", lastTimestamp);
            throw new RuntimeException(
                    String.format("Clock moved backwards. Refusing to generate id for %d milliseconds",
                            lastTimestamp - timestamp));
        }
        if (lastTimestamp == timestamp) {
            sequence = (sequence + 1) % sequenceMask;

            if (sequence == 0) {
                timestamp = tilNextMillis(lastTimestamp);
            }
        } else {
            //从0开始
            sequence = 0;
        }
        lastTimestamp=timestamp;
        Date date1 =new Date(lastTimestamp*1000);
        SimpleDateFormat sd =new SimpleDateFormat("yyyyMMddHHmmss");
        String format = sd.format(date1);
        String s = String.format("%06d", sequence);
        result=format+"-"+s;
        return result;
    }

    private long timeGen(){
        return System.currentTimeMillis()/1000;
    }
    /**
     * 等待到下一毫秒
     * @param lastTimestamp
     * @return
     */
    private long tilNextMillis(long lastTimestamp) {

        long timestamp = timeGen();

        while (timestamp <= lastTimestamp) {
            timestamp = timeGen();
        }
        return timestamp;
    }



    public static void main(String[] args) throws InterruptedException {

        MySnowFlake mySnowFlake =new MySnowFlake();
        MySnowFlake mySnowFlake2 =new MySnowFlake();
        HashSet<String> strings = new HashSet<>();
//        for (int i=0;i<100000;i++){
//
//
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    String s = mySnowFlake.nextId();
//                    System.out.println(s);
//                    strings.add(s);
//                }
//            }).start();
////            new Thread(new Runnable() {
////                @Override
////                public void run() {
////                    String s = mySnowFlake2.nextId();
////                    System.out.println(s);
////                    strings.add(s);
////                }
////            }).start();
//        }
        System.out.println(NetUtil.ipv4ToLong(NetUtil.getLocalhostStr()));
//        Thread.sleep(5000);
        System.out.println(strings.size());





    }
}


